//
//  CQTitleButton.m
//  01-OC微博
//
//  Created by 陈乾 on 15/10/26.
//  Copyright © 2015年 陈乾. All rights reserved.
//

#import "CQTitleButton.h"
#import "UIView+Frame.h"
#import "UIImage+CQImage.h"

//timeline_icon_more_highlighted
@implementation CQTitleButton

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        
        //设置背景  需要拉伸
        [self setBackgroundImage:[UIImage imageStretchableWithImageName:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted];
        //设置按钮的文字黑色
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return  self;
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //没有图片就不需要交换 文字和图片的位子
    if (self.currentImage == nil) {
        return;
    }
    
    //把文字移到前面 图片到后面
    self.titleLabel.x = 0;
    self.imageView.x = CGRectGetMaxX(self.titleLabel.frame)+2;
    
    

}

//扩展计算尺寸功能
-(void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    
    [self sizeToFit];
    
}

-(void)setImage:(UIImage *)image forState:(UIControlState)state
{
    [super setImage:image forState:state];
    [self sizeToFit];
}

@end
